package com.gxuwz.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.gxuwz.core.model.Cols;
import com.gxuwz.core.model.Page;
import com.gxuwz.core.util.MainUtil;
import com.gxuwz.core.util.UidUtils;
import com.gxuwz.core.web.action.BaseAction;
import com.gxuwz.pojo.DataEntity;
import com.gxuwz.pojo.Form;
import com.gxuwz.pojo.Topic;
import com.gxuwz.service.IDataService;
import com.gxuwz.service.IFormService;
import com.gxuwz.service.ITopicService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class WriterAction extends BaseAction implements Preparable, ModelDriven<Form>{


	protected static final String LIST_JSP = "/WEB-INF/page/writer/form_list.jsp";  
	protected static final String EDIT_JSP = "/WEB-INF/page/writer/data_edit.jsp"; 
	protected static final String LONG_EDIT_JSP = "/WEB-INF/page/writer/data_long_edit.jsp"; 

	@Autowired
	private IFormService formService;
	@Autowired
	private IDataService dataService;
	@Autowired
	private ITopicService topicService;
	
	private Page page;
	private Form form;
	private DataEntity dataEntity;
	private Map<String,String> dataMap;
	private Map<String,String[]> optionMap;
	private List<Topic> topicList;
	
	
	
	public String toTask(){
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	/**
	 * 获得当前用户的可填写任务
	 */
	public void getTask(){
		String userId = (String)getRequest().getSession().getAttribute("userId");
		List<Form> formList = formService.findByProperty("formState", "1");
		List<Form> taskList = new ArrayList<Form>();
		for(Form f:formList){
			if(dataService.isWriter("data_"+f.getId(), userId)){
				taskList.add(f);
			}
		}
		page = new Page();
		page.setCode(0);
		page.setData(taskList);
		page.setCount(taskList.size());
		reJson(page);
		return;
	}
	
	public String toEdit(){
		String userId = (String)getRequest().getSession().getAttribute("userId");
		dataEntity.setUserId(userId);
		form = formService.findById(form.getId());
		topicList = topicService.findByProperty("formId", form.getFormId());
		dataEntity = dataService.find("data_"+form.getId(), dataEntity, topicList).get(0);
		optionMap = MainUtil.getOptionMap(topicList);
		if(form.getFormType() != 2){
			setForwardView(EDIT_JSP);
		}else{
			setForwardView(LONG_EDIT_JSP);
		}
		
		return SUCCESS;
	}
	
	public void edit(){
		String userId = (String)getRequest().getSession().getAttribute("userId");
		dataEntity.setUserId(userId);
		form = formService.findById(form.getId());
		topicList = topicService.findByProperty("formId", form.getFormId());
		//多选需要进行处理
		for(Topic t : topicList){
			if(t.getTopicType() == 4){
				StringBuffer sb = new StringBuffer();
				String[] s = t.getOptions().split("；");
				int length = s.length;
				for(int i=0;i<length;i++){
					if(dataMap.get("data"+t.getId()+"_"+i) != null){
						sb.append(dataMap.get("data"+t.getId()+"_"+i));
						sb.append("；");
						dataMap.remove("data"+t.getId()+"_"+i);
					}
					if(i == length-1){
						sb.deleteCharAt(sb.length() - 1);
					}
				}
				dataMap.put("data"+t.getId(), sb.toString());
			}
		}
		dataMap.put("update_time", new Timestamp(new Date().getTime())+"");
		if(form.getFormType() != 2){
			dataService.update("data_"+form.getId(), dataMap);
		}else{
			//长期回收
			dataService.addData("data_"+form.getId(), dataMap);
		}
		
		
		return;
	}

	public Form getForm() {
		return form;
	}

	public void setForm(Form form) {
		this.form = form;
	}


	public DataEntity getDataEntity() {
		return dataEntity;
	}
	public void setDataEntity(DataEntity dataEntity) {
		this.dataEntity = dataEntity;
	}
	public Map<String, String> getDataMap() {
		return dataMap;
	}
	public void setDataMap(Map<String, String> dataMap) {
		this.dataMap = dataMap;
	}
	public List<Topic> getTopicList() {
		return topicList;
	}
	public void setTopicList(List<Topic> topicList) {
		this.topicList = topicList;
	}
	
	
	public Map<String, String[]> getOptionMap() {
		return optionMap;
	}
	public void setOptionMap(Map<String, String[]> optionMap) {
		this.optionMap = optionMap;
	}
	@Override
	public Form getModel() {
		// TODO Auto-generated method stub
		return form;
	}
	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		form = new Form();
		dataEntity = new DataEntity();
		dataMap = new HashMap<String, String>();
		topicList = new ArrayList<Topic>();
	}
	

	
	
	
}
